define("register", ["jquery", "md5", "jquery.validate", "additional-methods", "jquery.idcode"], function () {
    return {
        register: function () {
            //验证码
            $.idcode.setCode();
            $("#yzbtn").on("click", function () {
                $.idcode.setCode();
                return false
            })
            //验证码检查
            $("#Txtidcode").on("blur",function(){
                var IsBy = $.idcode.validateCode()  
                    if(IsBy){
                        $("#yzmsg").html("验证码正确")
                    }else{
                        $("#yzmsg").html("验证码错误")
                    }
            })
            //提交按钮状态控制
            btnstate()
            $("#bxty").on("click",function(){
                btnstate()
            })
            //密码强度检测
            $("#upwd").on("input", function () {
                var regruo1 = /^[0-9]{6,12}$/
                var regruo2 = /^[a-z]{6,12}$/i
                var regruo3 = /^(?![a-z]+$)[a-z0-9]{6,7}$/i
                var regzhong1 = /^[0-9]{13,16}$/
                var regzhong2 = /^[a-z]{13,16}$/i
                var regzhong3 = /^(?![a-z]+$)[0-9a-z]{8,12}$/i
                var regzhong4 = /^(?![~'!@#￥$%^&*()-+_=:]+$)[0-9a-z~'!@#￥$%^&*()-+_=:]{6,7}$/i
                var reggao1 = /^(?![a-z]+$)[0-9a-z]{13,16}$/i
                var reggao2 = /^(?![~'!@#￥$%^&*()-+_=:]+$)[0-9a-z~'!@#￥$%^&*()-+_=:]{8,16}$/i
                if (regruo1.test(this.value) || regruo2.test(this.value) || regruo3.test(this.value)) {
                    $("#state")[0].className = "ruo"
                    $("#state").html("弱")
                } else if (regzhong1.test(this.value) || regzhong2.test(this.value) || regzhong3.test(this.value) || regzhong4.test(this.value)) {
                    $("#state")[0].className = "zhong"
                    $("#state").html("中")
                } else if (reggao1.test(this.value) || reggao2.test(this.value)) {
                    $("#state")[0].className = "qiang"
                    $("#state").html("强")
                } else {
                    $("#state")[0].className = ""
                }
            })

            //表单验证
            $.validator.addMethod("unamereg", function (val, ele) {
                var phonereg = /^1[34578]\d{9}$/
                var emailreg = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/
                if (phonereg.test(val)) {
                    return true
                } else if (emailreg.test(val)) {
                    return true
                } else {
                    return false
                }
            }, "请输入有效的手机号或邮箱")

            $("form").validate({
                rules: {
                    uname: {
                        required: true,
                        minlength: 5,
                        unamereg: true,
                        remote : {
                            url : "php/isuname.php",
                            type:"post",
                            data:{
                                "uname":function(){return $("[name=uname]").val();}
                            }
                        }
                    },
                    upwd: {
                        required: true,
                        minlength: 6,
                        maxlength: 16
                    },
                    upwds:{
                        equalTo:"#upwd"
                    },
                },
                messages: {
                    uname: {
                        required: "用户名不能为空",
                        minlength: "不能少于5个字符",
                        remote:"用户名已存在"
                    },
                    upwd: {
                        required: "密码不能为空",
                        minlength: "密码长度不能少于6",
                        maxlength: "密码长度不能大于16"
                    },
                    upwds:{
                        equalTo:"两次密码不相同"
                    }
                },
                submitHandler: function () {
                        $.ajax({
                            url:"php/register.php",
                            type:"post",
                            data:{
                                uname:$("[name=uname]").val(),
                                upwd:$.md5($("[name=upwd]").val())
                            }
                        }).then(function(res){
                            console.log($("[name=uname]").val())
                           if(res.state=="1"){
                               $(".success").show()
                               $(".success span").html("注册成功")
                               $(".success b").html("是否立即登录?")
                               //立即登录
                               $(".subody a").eq(0).click(function () {
                                   $.ajax({
                                       url: "php/login.php",
                                       type: "post",
                                       data: {
                                           uname: $("[name=uname]").val(),
                                           upwd: $.md5($("[name=upwd]").val())
                                       }
                                   }).then(function (res) {
                                       if (res.state == "1"){
                                           window.location="index.html"
                                       }
                                   })
                               })
                               //取消
                               $(".subody a").eq(1).click(function () {
                                   $(".success").hide()
                               })

                           }else{
                            $(".success").show()
                            $(".success span").html("注册失败")
                            $(".success b").html("用户名已存在!")
                            setTimeout(function(){
                                $(".success").hide()
                            },3000)
                           }
                        })
                    
                    

                    return false
                }
            })
            //按钮状态控制方法
            function btnstate (){
                if($("#bxty").prop("checked")){
                    $("#btn").css("background","#ec3e7d")
                    $("#btn").prop("disabled",false)
                }else{
                    $("#btn").css("background","#ccc")
                    $("#btn").prop("disabled",true)
                }
            }




        }
    }
})